package cn.lutu.adapter.controller;

import cn.lutu.domain.service.RouteService;
import cn.lutu.protocol.dto.RoleUpdateInfoDTO;
import cn.lutu.protocol.dto.RouteAddDTO;
import cn.lutu.protocol.dto.RouteDTO;
import cn.lutu.protocol.dto.SpotAddDTO;
import cn.lutu.protocol.entity.Route;
import cn.lutu.protocol.vo.HotelVO;
import cn.lutu.protocol.vo.RouteVO;
import cn.lutu.protocol.vo.VoteVO;
import cn.lutu.util.pojo.vo.PageData;
import cn.lutu.util.response.ResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Slf4j
@Api(tags = "路线产品模块")
@RestController
@RequestMapping("/v1/routes/")
public class RouteController {
    @Autowired
    RouteService routeService;


    @ApiOperation(value = "添加路线产品功能")
    @PostMapping ("add")
    @PreAuthorize("hasAnyAuthority('/product/addNew')")
    public ResultVO insert(@RequestBody RouteDTO routeDTO){
        routeService.insert(routeDTO);
    return ResultVO.ok();
    }
    @ApiOperation("修改路线产品功能")
    @PostMapping("update")
    @PreAuthorize("hasAnyAuthority('/product/update')")
    public ResultVO update(@RequestBody RouteDTO routeDTO){
        routeService.update(routeDTO);
        return ResultVO.ok();
    }
    @ApiOperation(value = "删除路线产品功能")
    @PostMapping("/{id}/delete")
    @PreAuthorize("hasAnyAuthority('/product/delete')")
    public ResultVO delete(@PathVariable  Long id){
        routeService.delete(id);
        return ResultVO.ok();
    }

    @ApiOperation(value = "路线列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", dataType = "int"),
            @ApiImplicitParam(name = "queryType", value = "查询类型，当需要查询全部数据时，此参数值应该是all")
    })
    @GetMapping("/list")
    public ResultVO select(Integer page, String queryType){
        log.debug("开始处理【路线列表】请求，页码：{}", page);
        if (page == null) {
            page = 1;
        }
        Integer pageNum = page > 0 ? page : 1;
        PageData<RouteVO> pageData ;
        if ("all".equals(queryType)) {
            pageData = routeService.selectRouteList(1, Integer.MAX_VALUE);
        } else {
            pageData = routeService.selectRouteList(pageNum);
        }
        log.info("查询结束，查询结果为{}",pageData);
        return ResultVO.ok(pageData);
    }


    @ApiOperation(value = "查询路线详情")
    @GetMapping("/{id}/list")
    public ResultVO selectById(@PathVariable Long id){
       log.debug("根据ID查询路线详情数据，参数{}",id);
       RouteVO routeVO = routeService.selectRouteListById(id);
       return ResultVO.ok(routeVO);
}

    @ApiOperation(value = "路线价格")
    @GetMapping("/{id}/selectRoute")
    public ResultVO selectVoteById(@PathVariable Long id){
        log.debug("根据ID查询门票详情数据，参数：{}",id);
        List<RouteVO> list = routeService.selectRoute(id);
        return ResultVO.ok(list);

    }
    @PostMapping("/add-new")
    @ApiOperation("添加类别")
    @PreAuthorize("hasAnyAuthority('/product/addNew')")
    public ResultVO addSpot(@Valid RouteAddDTO routeAddDTO) {
        log.debug("开始处理【添加路线】的请求，参数：{}", routeAddDTO);
        routeService.addRoute(routeAddDTO);
        return ResultVO.ok();
    }
    @PostMapping("/{id}/deleteRoute")
    @ApiOperation("删除路线")
    @PreAuthorize("hasAnyAuthority('/product/delete')")
    public ResultVO deleteAuthorityById(@PathVariable Long id){
        log.info("开始执行删除路线请求");
        routeService.deleteRouteById(id);
        return ResultVO.ok();
    }
    @GetMapping("/{id:[0-9]+}")
    public ResultVO getInfoById(@PathVariable @Range(min = 1, message = "获取路线详情失败，请提交合法的ID值！") Long id) {
        log.debug("开始处理【根据ID查询路线数据】的请求，参数：{}", id);
        Route route = routeService.selectRouteById(id);
        return ResultVO.ok(route);
    }
    @ApiOperation("修改路线")
    @PostMapping("/{id:[0-9]+}/update")
    @PreAuthorize("hasAnyAuthority('/product/update')")
    public ResultVO updateInfoById(@Validated RouteDTO routeDTO) {
        log.debug("开始处理【修改用户角色】的请求，参数：{}", routeDTO);
        routeService.updateRoute(routeDTO);
        return ResultVO.ok();
    }



}
